﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Input;

using Microsoft.Practices.Composite.Events;
using Microsoft.Practices.Composite.Presentation.Regions;

using Supremacy.Annotations;
using Supremacy.Client;

namespace SupremacyEditor.RacesModule
{
    /// <summary>
    /// Interaction logic for RaceDatabaseView.xaml
    /// </summary>
    public partial class RaceDatabaseView : IRaceDatabaseView, INotifyPropertyChanged
    {
        public RaceDatabaseView()
        {
            InitializeComponent();
        }

        private void RaceListView_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            InvokeRaceSelected(
                new DataEventArgs<IRacePresentationModel>(
                    RaceListView.SelectedItem as IRacePresentationModel));
            OnPropertyChanged("SelectedRace");
        }

        #region Implementation of IRaceDatabaseView
        private IEnumerable<IRacePresentationModel> _races;
        public void ShowView()
        {
            var regionManager = RegionManager.GetRegionManager(this);
            if (regionManager != null)
                regionManager.Regions[RegionNames.DockSite].Activate(this.Parent);
        }

        public IEnumerable<IRacePresentationModel> Races
        {
            get { return _races; }
            set
            {
                if (Equals(_races, value))
                    return;
                _races = value;
                if ((_races != null) && !CollectionViewSource.GetDefaultView(_races).SortDescriptions.Any())
                {
                    CollectionViewSource.GetDefaultView(_races).SortDescriptions.Add(
                        new SortDescription(
                            "Header",
                            ListSortDirection.Ascending));
                }
                this.DataContext = Races;
                OnPropertyChanged("Races");
            }
        }

        public IRacePresentationModel SelectedRace
        {
            get { return RaceListView.SelectedItem as IRacePresentationModel; }
            set { RaceListView.SelectedItem = value; }
        }

        public event EventHandler<DataEventArgs<IRacePresentationModel>> RaceSelected;

        private void InvokeRaceSelected(DataEventArgs<IRacePresentationModel> e)
        {
            var raceSelectedEventHandler = RaceSelected;
            if (raceSelectedEventHandler != null)
                raceSelectedEventHandler(this, e);
        }
        #endregion

        #region Implementation of INotifyPropertyChanged
        protected void OnPropertyChanged([NotNull] string propertyName)
        {
            if (propertyName == null)
                throw new ArgumentNullException("propertyName");
            if (PropertyChanged != null)
                PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
        }

        public event PropertyChangedEventHandler PropertyChanged;
        #endregion

        private void RaceListView_MouseDoubleClick(object sender, MouseButtonEventArgs e)
        {
            var item = WpfHelper.FindItemsControlItemFromVisualDescendant(
                e.OriginalSource as DependencyObject) as IEditorItem;
            if (item != null)
                EditorCommands.OpenItem.Execute(item);
        }

        private void RaceListView_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.Key == Key.Enter)
            {
                var item = RaceListView.SelectedItem as IEditorItem;
                if (item != null)
                    EditorCommands.OpenItem.Execute(item);
            }
        }
    }
}
